package co.fitstart.admin.web.controller.report;

import java.util.Date;
import java.util.List;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.DateUtils;

import co.fitstart.admin.entity.search.SigninReportSearch;
import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.CoachService;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.service.ReportService;
import co.fitstart.admin.service.SiteService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.admin.web.view.ExcelTemplateView;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;

/**
 * 未签到报表
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("/signin-reports")
@MenuSetting("menu-report")
public class SigninReportController {
    @Autowired
    private SiteService siteService;
    
    @Autowired
    private CoachService coachService;
    
    @Autowired
    private ReportService reportService;
    
    @Autowired
    private PrincipalService principalService;
    
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-signin-report")
    public ModelAndView list(@Secure Login login,
                             SigninReportSearch search,
                             @RequestParam(name = "beginDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,
                             @RequestParam(name = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                             @RequestParam(name = "siteId", required = false) Long siteId,
                             @RequestParam(name = "coachId", required = false) Long coachId) {
        ModelAndView modelAndView = new ModelAndView("report/signin-report-list");
        
        if(beginDate == null || endDate == null) {
            Date now = DateUtils.parseDate(DateFormatUtils.format(new Date(), "yyyy-MM-dd"), "yyyy-MM-dd");
            beginDate = DateUtils.addDays(now, -6);
            endDate = now;
        }
        
        Site site = null;
        if(!login.hasSite() && siteId != null) {
            site = siteService.load(siteId);
        }else if (login.hasSite()){
            site = login.getSite();
        }
        
        if(site == null && (login.isAreaManager() || login.isAreaServer())) {
            Principal principal = principalService.load(Principal.class, login.getId());
            
            List<Site> sites = siteService.listSite(principal);
            
            search.setSites(sites);
        }
        
        Principal coach = null;
        if(coachId != null) {
            coach = coachService.load(coachId);
        }
        
        search.setBegin(beginDate);
        search.setEnd(endDate);
        search.setSite(site);
        search.setCoach(coach);
        
        if(search.getSites() == null || !search.getSites().isEmpty()) {
            List<CourseRound> courseRounds = reportService.search(search);
            
            modelAndView.addObject("courseRounds", courseRounds);
        }
       
        modelAndView.addObject("beginDate", DateFormatUtils.format(beginDate, "yyyy-MM-dd"));
        modelAndView.addObject("endDate", DateFormatUtils.format(endDate, "yyyy-MM-dd"));
        modelAndView.addObject("today", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
        
        return modelAndView;
    }
    
    /**
     * 
     * @return
     */
    @RequestMapping(value = "/export-view", method = RequestMethod.GET)
    public ModelAndView export() {
        ModelAndView modelAndView = new ModelAndView("report/signin-report-export");
        
        modelAndView.addObject("today", DateFormatUtils.format(new Date(), "yyyy-MM-dd"));
        
        return modelAndView;
    }
    
    /**
     * 
     * @param login
     * @param search
     * @param beginDate
     * @param endDate
     * @param siteId
     * @param coachId
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView onExport(@Secure Login login,
                                 SigninReportSearch search,
                                 @RequestParam(name = "beginDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginDate,
                                 @RequestParam(name = "endDate", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
                                 @RequestParam(name = "siteId", required = false) Long siteId,
                                 @RequestParam(name = "coachId", required = false) Long coachId) {
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("signin-report.xls"));
        
        if(beginDate == null || endDate == null) {
            Date now = DateUtils.parseDate(DateFormatUtils.format(new Date(), "yyyy-MM-dd"), "yyyy-MM-dd");
            beginDate = DateUtils.addDays(now, -6);
            endDate = now;
        }
        
        Site site = null;
        if(!login.hasSite() && siteId != null) {
            site = siteService.load(siteId);
        }else if (login.hasSite()){
            site = login.getSite();
        }
        
        if(site == null && (login.isAreaManager() || login.isAreaServer())) {
            Principal principal = principalService.load(Principal.class, login.getId());
            
            List<Site> sites = siteService.listSite(principal);
            
            search.setSites(sites);
        }
        
        Principal coach = null;
        if(coachId != null) {
            coach = coachService.load(coachId);
        }
        
        search.setBegin(beginDate);
        search.setEnd(endDate);
        search.setSite(site);
        search.setCoach(coach);
        
        if(search.getSites() == null || !search.getSites().isEmpty()) {
            List<CourseRound> courseRounds = reportService.search(search);
            
            modelAndView.addObject("courseRounds", courseRounds);
        }
        
        modelAndView.addObject("beginDate", DateFormatUtils.format(beginDate, "yyyy-MM-dd"));
        modelAndView.addObject("endDate", DateFormatUtils.format(endDate, "yyyy-MM-dd"));
        modelAndView.addObject("site", site);
        modelAndView.addObject("coach", coach);
        modelAndView.addObject("name", search.getName());
        modelAndView.addObject("mobile", search.getMobile());
        
        return modelAndView;
    }
}
